package com.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.bean.wd_ht_build;
import com.bean.wd_ht_project;
import com.service.BuildService;
import com.service.ProjectService;

@Controller
@RequestMapping("/build")
public class BuildAction {
	@Autowired
	private BuildService bulidservice;
	@Autowired
	private ProjectService projectservice;
	/**
	 * 查询所有的楼栋信息
	 * @param model 传参
	 * @return
	 */
	@RequestMapping(value="/selectall.do")
	public String selectAll(Model model){
		List<wd_ht_build> list = bulidservice.selectAllToProject();
		model.addAttribute("list",list);
		System.out.println("查询楼栋信息-------------------"+list);
		return "BuildManager";
	} 
	/**
	 * 跳转到添加楼栋页面
	 * @param model 传参
	 * @return
	 */
	@RequestMapping(value="/add.do")
	public String add(Model model){
		List<wd_ht_project> plist = projectservice.selectAll();//查询所有的项目信息，将项目名称和id传到前台
		model.addAttribute("plist",plist);
		return "BuildAddManager";
	}
	/**
	 * 添加楼栋信息
	 * @param bbean 添加的楼栋信息
	 * @return
	 */
	@RequestMapping(value="/insert.do")
	public String insert(wd_ht_build bbean){
		System.out.println("添加楼栋----------------------------------"+bbean);
		int num = bulidservice.insert(bbean);
		return "redirect:selectall.do";
	}
	/**
	 * 批量删除(可以删一个)
	 * @param buildIdArray 前台传来所有需要删除的楼栋id
	 * @return
	 */
	@RequestMapping(value="/deletebyprimarykey.do")
	public String deleteByPrimaryKey(String[] buildIdArray){
		for(String s:buildIdArray){
			System.out.println("楼栋id----------------------------------"+s);
			int num = bulidservice.deleteByPrimaryKey(s);
		}
		return "redirect:selectall.do";
	}
	/**
	 * 跳转到修改页面
	 * @param projectId 要修改的项目id
	 * @param model 传参
	 * @return
	 */
	@RequestMapping(value="/selectone.do")
	public String selectone(String buildId,Model model){
		System.out.println("跳转添加页面楼栋id----------------------------------"+buildId);
		wd_ht_build bbean = bulidservice.selectByPrimaryKey(buildId);
		model.addAttribute("bbean",bbean);
		List<wd_ht_project> plist = projectservice.selectAll();//查询所有的项目信息，将项目名称和id传到前台
		model.addAttribute("plist",plist);
		return "BuildUpdateManager";
	}
	/**
	 * 修改楼栋信息
	 * @param pbean 修改之后的楼栋信息
	 * @return
	 */
	@RequestMapping(value="/update.do")
	public String update(wd_ht_build bbean){
		bbean.setUpdateUser("修改人从session中拿！");//写session中修改人的信息
		Date day = new Date();
		SimpleDateFormat fm = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			bbean.setUpdateTime(fm.parse(fm.format(day)));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		System.out.println("修改后的楼栋信息----------------------------------"+bbean);
		int num = bulidservice.updateByPrimaryKeySelective(bbean);
		return "redirect:selectall.do";
	}
}
